package com.fortune.conf;

/**
 * @program: fortune
 * @description:
 * @author: wwb
 * @create: 2025-04-21 16:17
 */
import org.openqa.selenium.JavascriptExecutor;

import java.io.*;
import java.net.URL;
import java.net.URLConnection;
import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;
import java.util.Random;

public class ImageDownloader {
    public static void download(String urlString, String savePath) throws IOException {
        URL url = new URL(urlString);
        URLConnection conn = url.openConnection();
        conn.setRequestProperty("Referer", "https://www.toutiao.com/");
        try (InputStream in = url.openStream();
             ReadableByteChannel rbc = Channels.newChannel(in);
             FileOutputStream fos = new FileOutputStream(savePath)) {
            fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
        }
    }

    public static void main(String[] args) throws IOException {
//        // 随机滚动模式
//        JavascriptExecutor js = (JavascriptExecutor)driver;
//        for (int i = 0; i < 5; i++) {
//            int scrollHeight = new Random().nextInt(500) + 300;
//            js.executeScript("window.scrollBy(0, " + scrollHeight + ")");
//            Thread.sleep(1500 + new Random().nextInt(1000));
//        }

            String imgUrl = "http://p3-sign.toutiaoimg.com/tos-cn-i-axegupay5k/4a2943207cd4409cb8b88c6d52bd9332~tplv-tt-origin-web:gif.jpeg?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1745827525&x-signature=d5GPS7yWZylCC0bvh7FWwG0Phbw%3D";
            String filename = "E:\\images\\fortune\\fc_image\\2025\\4\\26\\image1.jpg";
            ImageDownloader.download(imgUrl, filename);
    }
}